package com.library.management.service.impl;

import java.util.List;

import com.library.common.core.domain.model.LoginUser;
import com.library.common.utils.DateUtils;
import com.library.common.utils.SecurityUtils;
import com.library.common.utils.StringUtils;
import com.library.management.domain.LibraryBorrow;
import com.library.management.service.ILibraryBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.library.management.mapper.LibraryBookMapper;
import com.library.management.domain.LibraryBook;
import com.library.management.service.ILibraryBookService;

/**
 * 图书Service业务层处理
 *
 * @author library
 * @date 2023-03-26
 */
@Service
public class LibraryBookServiceImpl implements ILibraryBookService {
    @Autowired
    private LibraryBookMapper libraryBookMapper;
    @Autowired
    private ILibraryBorrowService libraryBorrowService;

    /**
     * 查询图书
     *
     * @param id 图书主键
     * @return 图书
     */
    @Override
    public LibraryBook selectLibraryBookById(Long id) {
        return libraryBookMapper.selectLibraryBookById(id);
    }

    /**
     * 查询图书列表
     *
     * @param libraryBook 图书
     * @return 图书
     */
    @Override
    public List<LibraryBook> selectLibraryBookList(LibraryBook libraryBook) {


        return libraryBookMapper.selectLibraryBookList(libraryBook);
    }

    /**
     * 新增图书
     *
     * @param libraryBook 图书
     * @return 结果
     */
    @Override
    public int insertLibraryBook(LibraryBook libraryBook) {
        libraryBook.setCreateTime(DateUtils.getNowDate());
        return libraryBookMapper.insertLibraryBook(libraryBook);
    }

    /**
     * 修改图书
     *
     * @param libraryBook 图书
     * @return 结果
     */
    @Override
    public int updateLibraryBook(LibraryBook libraryBook) {
        libraryBook.setUpdateTime(DateUtils.getNowDate());
        return libraryBookMapper.updateLibraryBook(libraryBook);
    }

    /**
     * 批量删除图书
     *
     * @param ids 需要删除的图书主键
     * @return 结果
     */
    @Override
    public int deleteLibraryBookByIds(String[] ids) {
        return libraryBookMapper.deleteLibraryBookByIds(ids);
    }

    /**
     * 删除图书信息
     *
     * @param id 图书主键
     * @return 结果
     */
    @Override
    public int deleteLibraryBookById(String id) {
        return libraryBookMapper.deleteLibraryBookById(id);
    }

    @Override
    public int borrowBooks(String[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isEmpty(ids)){
            return 0;
        }
        List<LibraryBook> books = selectLibraryBookByIds(ids);
        books.forEach(book->{
            if (book.getBookSum()>book.getBookLend()) {
                book.setBookLend(book.getBookLend() + 1);
                libraryBookMapper.updateLibraryBook(book);
                //生成借阅记录
                LibraryBorrow libraryBorrow = new LibraryBorrow();
                libraryBorrow.setReaderId(loginUser.getUserId());
                libraryBorrow.setBookId(book.getId());
                libraryBorrow.setBorrowTime(DateUtils.getNowDate());
                libraryBorrow.setCreateBy(String.valueOf(loginUser.getUserId()));
                libraryBorrow.setBorrowTime(DateUtils.getNowDate());
                libraryBorrowService.insertLibraryBorrow(libraryBorrow);
            }
        });
        return 1;
    }

    @Override
    public List<LibraryBook> selectLibraryBookByIds(String[] ids) {
        return libraryBookMapper.selectLibraryBookByIds(ids);
    }
}
